<?php
/* ***********************************************************************
 * File: 				project.class.php
 * Author: 				Micheal Luthor
 * Email: 				micheal.the.ranidae@gmail.com
 * Version: 			0.0.0
 * Lasr Modified At: 	2013-03-28 09:59
 * Dependence: 			database.interface.php
 * Describe: 			We use this to manage the project information.
 * ToDo:
 * ***********************************************************************/

class Project
{
	/**
	 * To create a project, only name and describe are required. 
	 *
	 * @param 	string	$name	The name of the project.
	 * @param	string	$desc	The describe of the project.
	 * @return 	int				>=0 (The id of the project.)
	 *							<0	(The error code of the process.)
	 */
	public static function Create( $name, $desc  )
	{
		
	}
	
	public static function set(){}
	public static function get(){}
	public static function delete(){}
	public static function find(){}
	
	const STATE_NEW = 0;
	const STATE_START = 1;
	const STATE_PAUSE = 2;
	const STATE_STOP = 3;
	const STATE_FINISHED = 4;

	public static function setState(){}
	public static function getState(){}

	private static $SQL_CREATE_TABLE = 
			'CREATE TABLE `ranidae`.`project` (
			`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'The id of the project\',
			`name` VARCHAR( 1024 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT \'The name of the project.\',
			`describe` VARCHAR( 4096 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT \'The describe of the project.\',
			`state` INT UNSIGNED NULL DEFAULT \'0\' COMMENT \'0:new, 1:start, 2:pause, 3:stop, 4:finished\',
			`started_at` DATETIME NULL DEFAULT \'0000-00-00 00:00:00\' COMMENT \'The time of the project started, 0 means not start.\',
			`end_at` DATETIME NULL DEFAULT \'0000-00-00 00:00:00\' COMMENT \'The time of the project ends. 0 means nerver.\',
			`organiger` INT UNSIGNED NOT NULL COMMENT \'The user\\\'s id who create this.\',
			`created_at` DATETIME NOT NULL COMMENT \'The time when project was created.\',
			`visibility` VARCHAR( 1024 ) NULL DEFAULT \'(all)\' COMMENT \'who can see it. (all|friends|me|(userid1,userid2,...))\',
			`joinable` VARCHAR( 1024 ) NULL DEFAULT \'(all)\' COMMENT \'who can join it. (all|friends|me|(userid1,userid2,...))\',
			INDEX ( `name` ) ,
			FULLTEXT ( `describe` )
			) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT = \'This table use to manage the project.\'';
	
	/** 
	 * This function should only call once when user install the server. 
	 *
	 * @return	boolean		true: OK, false:Failed	
	 */
	public static function serviceInit()
	{
		global $db;
		
		$tables = $db->ListTables();
		if ( false === $tables )
		{
			die('Something wrong with the database, so project sevice could not create table.');
		}
		
		/* If tables already, we need to do nothing about the servic. */
		if ( in_array('project', $tables) )
		{
			return true;
		}
		
		/* Create the table. */
		$db->Query(self::$SQL_CREATE_TABLE);
		if (0 != $db->errno() )
		{
			die('Something wrong with the database, so project sevice could not create table.');
		}
		
		return true;
	}
}

require_once('../library/database/Mysql.class.php');
$db->Connect("localhost","root","","");
$db->Set('char set', "UTF8");
$db->Set('time zone', "+8:00");
$db->Set('db', "ranidae");

// Project::serviceInit();
?>
